
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rt.schedulebase.dto.DictRoleFunctionDto;
import com.rt.schedulebase.entity.DictExamClass;
import com.rt.schedulebase.entity.DictRoleFunction;
import com.rt.schedulebase.mapper.DictRoleFunctionMapper;
import com.rt.schedulenew.api.IDictRoleFunctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulenew.utils.base.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictRoleFunctionServiceImpl extends ServiceImpl<DictRoleFunctionMapper, DictRoleFunction> implements IDictRoleFunctionService {

    @Override
    public List<DictRoleFunction> getList(DictRoleFunctionDto dto) {
        QueryWrapper<DictRoleFunction> qw = new QueryWrapper();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("EXAM_CLASS", dto.getExamClass());
        map.put("ROLE_ID", dto.getRoleId());
        map.put("FUN_ID", dto.getFunId());
        qw.allEq((Map) map, false);
        List<DictRoleFunction> list = (List<DictRoleFunction>) baseMapper.selectList((Wrapper) qw);
        return list;
    }

    @Override
    public List<DictRoleFunction> getListAll() {
        return (List<DictRoleFunction>) baseMapper.selectList((Wrapper) null);
    }

    @Override
    public DictRoleFunction getInfo(DictRoleFunctionDto dto) {
        QueryWrapper<DictRoleFunction> qw = new QueryWrapper();
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dto.getRoleId());
        map.put("EXAM_CLASS", dto.getExamClass());
        map.put("FUN_ID", dto.getFunId());
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public int getCount(DictRoleFunctionDto dto) {
        return baseMapper.getCount(dto);
    }

    @Override
    public List<DictRoleFunctionDto> getRoleFunctionList(DictRoleFunctionDto dto) {
        return baseMapper.getRoleFunctionList(dto);
    }

    @Override
    public List<DictExamClass> getFunctionExamClass(DictRoleFunctionDto dto) {
        return baseMapper.getFunctionExamClass(dto);
    }

    @Override
    public boolean insertBatch(DictRoleFunction dictRoleFunction) {
        List<String> funIds = CommonUtil.split2List(dictRoleFunction.getFunId());
        for (String funId : funIds) {
            dictRoleFunction.setFunId(funId);
            baseMapper.insert(dictRoleFunction);
        }
        return true;
    }


    @Override
    public boolean deleteByCondition(DictRoleFunction dictRoleFunction) {
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dictRoleFunction.getRoleId());
        map.put("EXAM_CLASS", dictRoleFunction.getExamClass());
        map.put("LIMIT_DAYS", dictRoleFunction.getLimitDays());
        UpdateWrapper<DictRoleFunction> uw = new UpdateWrapper<>();
        uw.allEq(map, false);
        if (StringUtils.isNotBlank(dictRoleFunction.getFunId())) {
            uw.in("FUN_ID", CommonUtil.split2List(dictRoleFunction.getFunId()));
        }
        return retBool(baseMapper.delete(uw));
    }
}
